package com.wx.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.net.httpserver.HttpsServer;
import com.wx.model.PageModel;
import com.wx.model.ProductModel;
import com.wx.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/back/producats")
public class productsController {
    @Autowired
    IProductsService iProductsService;

    @RequestMapping("/producatsFindAll")
    public JSONObject producatsFindAll(@RequestBody PageModel pageModel){
        pageModel.setPageNum(pageModel.getPageNum()==null || "".equals(pageModel.getPageNum())? 1 :(pageModel.getPageNum()));
        pageModel.setPageSize(pageModel.getPageSize()==null || "".equals(pageModel.getPageSize()) ? 10:(pageModel.getPageSize()));
        List<ProductModel> list=iProductsService.producatsFindAll(pageModel);
        int number=iProductsService.getCount(pageModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        return jsonObject;
    }

    @RequestMapping("/producatsFindTh")
    public JSONObject producatsFindTh(@RequestBody ProductModel productModel){
        List<ProductModel> list=iProductsService.producatsFindTh(productModel);
        int number=iProductsService.getThCount(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        return jsonObject;
    }
    @RequestMapping("/producatsAdd")
    public JSONObject producatsAdd(@RequestBody ProductModel productModel){
        int count=iProductsService.producatsAdd(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }


    @RequestMapping("/producatsFindById")
    public JSONObject producatsFindById(@RequestBody ProductModel productModel, HttpSession session) {
        ProductModel productModel1 = iProductsService.producatsFindById(productModel);
        session.setAttribute("logo",productModel1.getImgHref());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productModel", productModel1);
        return jsonObject;
    }

    @RequestMapping("/producatsUpdate")
    public JSONObject producatsUpdate(@RequestBody ProductModel productModel){
        int count=iProductsService.producatsUpdate(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }

    @RequestMapping("/deleteById")
    public JSONObject deleteById(@RequestBody ProductModel productModel){
        int count=iProductsService.deleteById(productModel);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("count",count);
        return jsonObject;
    }
}
